package com.tanhua.server.service;

import com.tanhua.commons.templates.HuanXinTemplate;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.vo.ContactVo;
import com.tanhua.domain.vo.MessageVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.dubbo.api.mongo.FriendApi;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.utils.RelativeDateFormat;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 环信即时通信
 */
@Service
public class IMService {

    @Reference
    private FriendApi friendApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private CommentApi commentApi;

    /**
     * 点击聊一聊后添加好友
     *
     * @param userId
     */
    public void makeFriends(Long userId) {
        //获取登录id
        Long loginUserId = UserHolder.getUserId();

        //调用api添加
        friendApi.makeFriends(loginUserId, userId);

        //调用环信添加好友：必须在环信里面也是好友，不然会报错404
        huanXinTemplate.makeFriends(loginUserId, userId);


    }

    /**
     * 联系人列表
     *
     * @param page
     * @param pageSize
     * @param keyword
     * @return
     */
    public PageResult<ContactVo> queryContactsList(Long page, Long pageSize, String keyword) {

        //1.调用api查询好友列表
        PageResult pageResult = friendApi.findPage(UserHolder.getUserId(), page, pageSize);
        List<Friend> friendList = pageResult.getItems(); //获取结果集

        List<ContactVo> VoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(friendList)) { //结果集有数据在进行查询
            //2.获取所有好友列表的id
            List<Long> longList = friendList.stream().map(Friend::getFriendId).collect(Collectors.toList());
            //查询所有好友的信息
            List<UserInfo> userInfoList = userInfoApi.findByBatchId(longList);
            VoList = userInfoList.stream().map(userInfo -> {
                //构建vo
                ContactVo vo = new ContactVo();
                //复制属性
                BeanUtils.copyProperties(userInfo, vo);
                vo.setUserId(userInfo.getId().toString());
                return vo;
            }).collect(Collectors.toList());

        }
        //返回
        pageResult.setItems(VoList);
        return pageResult;
    }


    /**
     * 谁对登录用户点赞通知
     *
     * @param commentType 1:点赞，2：评论，3：喜欢
     * @param page
     * @param pagesize
     * @return
     */
    public PageResult<MessageVo> messageCommentList(int commentType, Long page, Long pagesize) {
        //查询登录用户评论表得到所有的评论人
        PageResult commentPageResult = commentApi.findPageByUserId(UserHolder.getUserId(), commentType, page, pagesize);
        List<Comment> CommentList = commentPageResult.getItems();

        List<MessageVo> voList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(CommentList)) {
            //获取所有的评论人的id
            List<Long> longListId = CommentList.stream().map(Comment::getUserId).collect(Collectors.toList());

            //查询所有的评论人信息
            List<UserInfo> userInfoList = userInfoApi.findByBatchId(longListId);

            //把所有评论人信息转成map
            Map<Long, UserInfo> userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getId, u -> u));

            //复制属性
          voList = CommentList.stream().map(comment -> {
                //构建 MessageVo
                MessageVo vo = new MessageVo();
                //获取评论人信息
                UserInfo userInfo = userInfoMap.get(comment.getUserId());

                BeanUtils.copyProperties(userInfo, vo);

                //todo 为啥不用  Comment里面的时间
                vo.setCreateDate(RelativeDateFormat.format(new Date(comment.getCreated()))); //评论时间

                vo.setId(userInfo.getId().toString()); //评论人
                return vo;

            }).collect(Collectors.toList());


        }
        commentPageResult.setItems(voList);

        return commentPageResult;
    }

}
